//
//  BCUserManager.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/11/29.
//  Copyright © 2018 guoshikeji. All rights reserved.
//

#import "BCUserManager.h"

#import <SAMKeychain.h>

#define UserLoginDataDefaultKey @"UserLgoinDataDefault"


//纪录上次用户区域
#define LastLaunchReginId @"LastLaunchReginId"

NSString * const BCUserManagerUpdateUserLoginDataNotification = @"userManagerUpdateUserLoginDataNotification";
//用户登录过期发出通知
NSString * const BCUserManagerUserLoginOverdueNotification = @"userManagerUserLoginOverdueNotification";
//登录用户信息清空发出通知
NSString * const BCUserManagerClearLoginUserNotification = @"userManagerClearLoginUserNotification";

@interface BCUserManager () {
    UserLoginDataModel *_varUserLogin;
}

@end

@implementation BCUserManager

static BCUserManager *userManager = nil;
+ (instancetype)sharedUser {
    static BCUserManager *userManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        userManager = [[[self class] alloc] init];
    });
    return userManager;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        /**
         初始化
         登录用户是否过期的管理者
         */
        _userOverdue = [[BCUserOverdueManager alloc] init];
        //
        id obj = [[NSUserDefaults standardUserDefaults] objectForKey:UserLoginDataDefaultKey];
        //
        if (obj) {
            _varUserLogin = [UserLoginDataModel yy_modelWithJSON:obj];
            //如果有登录用户判断登录是否过期 过期就清空登录信息
        }
        //上次运行时的regin
        id lastLaunchReginId = [[NSUserDefaults standardUserDefaults] objectForKey:LastLaunchReginId];
        if ([lastLaunchReginId isKindOfClass:NSString.class]) {
            _reg_id = lastLaunchReginId;
        }
        
        //开始监听有效期
        [_userOverdue startMonitoring];
    }
    return self;
}

- (BOOL)isLogin {
    return _varUserLogin==nil?false:true;
}
- (void)logout {
    [self clearLoginUser];
}
/**
 清空当前登录用户数据 在请求接口时返回用户权限错误后调用
 */
- (void)clearLoginUser {
    _varUserLogin = nil;
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:UserLoginDataDefaultKey];
    //
    [self clearCacheData];
    
    //
    [[NSNotificationCenter defaultCenter] postNotificationName:BCUserManagerClearLoginUserNotification object:nil];
}


- (UserLoginDataModel *)userLogin {
    return _varUserLogin;
}
- (UserInfoModel *)userInfo {
    return _varUserLogin.userInfo;
}
//
- (void)updateUserLoginData:(UserLoginDataModel *)data {
    if (data) {
        _varUserLogin = data;
        id obj = [_varUserLogin yy_modelToJSONObject];
        [[NSUserDefaults standardUserDefaults] setObject:obj forKey:UserLoginDataDefaultKey];
        //
        [[BCUserLoginManager sharedLogin] updateLastLoginUserToInfos:data.userInfo];
        //
        [[NSNotificationCenter defaultCenter] postNotificationName:BCUserManagerUpdateUserLoginDataNotification object:nil];
    }
}


- (void)userLoginSuccessUpdateUserLoginData:(UserLoginDataModel *)data {
    [self updateUserLoginData:data];
}

#pragma mark - 指纹登录或者面容登录处理
/**
 当前登录用户是否已经开启指纹或面容登录
 */
- (BOOL)isOpenLocalAuthentication {
    return [[BCUserLoginManager sharedLogin] isOpenLocalAuthenticationWithUserId:self.userLogin.user_id];
}




#pragma mark - others

- (void)clearCacheData {
    _walletData = nil;
    _safeCenterData = nil;
}


#pragma mark - 首页区域 reg_id

- (void)setReg_id:(NSString *)reg_id {
    _reg_id = reg_id;
    //
    if (reg_id.length) {
        [[NSUserDefaults standardUserDefaults] setObject:reg_id forKey:LastLaunchReginId];
    }else {
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:LastLaunchReginId];
    }
}

@end
